package com.dingreading.cloud.mall.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.mall.dto.DeliveryAddressDto;
import com.dingreading.cloud.mall.entity.DeliveryAddress;
import com.dingreading.cloud.mall.service.DeliveryAddressService;
import com.dingreading.cloud.mall.util.OrderFormV3Util;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/front/deliveryAddress")
@Api(tags = "商城-送货地址管理")
public class FrontDeliveryAddressController {

    @Resource
    private DeliveryAddressService deliveryAddressService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @RequestParam(value = "name", required = false) @ApiParam(value = "用户名/手机号") String name,
            @RequestParam(value = "status", required = false) @ApiParam(value = "默认状态 1.是 0.不是") Integer status
    ) {
        Page<DeliveryAddress> pageList = deliveryAddressService.pageList(pageUtil, storeUid, staffUid, name, status);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(pageList.getTotalRow());
        data.setList(pageList.getRecords());
        return R.ok(data);
    }

    @ApiOperation(value = "添加/编辑送货地址")
    @PostMapping(value = "/editBean", produces = "application/json; charset=UTF-8")
    public R<Object> editBean(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @RequestParam(value = "id", required = false) @ApiParam(value = "商品id") Long id,
            @RequestParam(value = "name", required = false) @ApiParam(value = "姓名") String name,
            @RequestParam(value = "phone", required = false) @ApiParam(value = "手机号") String phone,
            @RequestParam(value = "provinceCode", required = false) @ApiParam(value = "省份编码") String provinceCode,
            @RequestParam(value = "cityCode", required = false) @ApiParam(value = "城市编码") String cityCode,
            @RequestParam(value = "districtCode", required = false) @ApiParam(value = "区域编码") String districtCode,
            @RequestParam(value = "province", required = false) @ApiParam(value = "省份名称") String province,
            @RequestParam(value = "city", required = false) @ApiParam(value = "城市名称") String city,
            @RequestParam(value = "district", required = false) @ApiParam(value = "区域名称") String district,
            @RequestParam(value = "address", required = false) @ApiParam(value = "详细地址") String address,
            @RequestParam(value = "status", required = false) @ApiParam(value = "状态 1.启用 0.禁用") Integer status
    ) {
        if (id == null || id <= 0) {
            // 新增
            DeliveryAddress data = OrderFormV3Util.newDeliveryAddress(
                    new DeliveryAddress(), storeUid, staffUid, name, phone,
                    status, address, provinceCode, province, cityCode, city, districtCode, district
            );
            deliveryAddressService.saveOrUpdate(data);
            if (data.getStatus() != null && data.getStatus().equals(1)) {
                // 默认地址设置
                deliveryAddressService.updateStatus(storeUid, staffUid, data.getId());
            }
        } else {
            // 修改
            DeliveryAddress data = deliveryAddressService.getById(id);
            if (data == null) return R.fail("获取失败");

            OrderFormV3Util.newDeliveryAddress(
                    data, storeUid, staffUid, name, phone, status, address,
                    provinceCode, province, cityCode, city, districtCode, district
            );
            deliveryAddressService.saveOrUpdate(data);
            if (data.getStatus() != null && data.getStatus().equals(1)) {
                // 默认地址设置
                deliveryAddressService.updateStatus(storeUid, staffUid, data.getId());
            }
        }
        return R.ok();
    }

    @ApiOperation(value = "获取地址详情")
    @PostMapping(value = "/get", produces = "application/json; charset=UTF-8")
    public R<Object> get(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        DeliveryAddress address = deliveryAddressService.getById(id);
        if (address == null) return R.fail("获取失败");

        DeliveryAddressDto addressDto = OrderFormV3Util.getAddressDto(address);

        return R.ok(addressDto);
    }

    @ApiOperation(value = "设置默认地址")
    @PostMapping(value = "/updateStatus", produces = "application/json; charset=UTF-8")
    public R<Object> updateStatus(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        this.deliveryAddressService.updateStatus(storeUid, staffUid, id);
        return R.ok();
    }

    @ApiOperation(value = "获取默认地址")
    @PostMapping(value = "/getDefaultAddress", produces = "application/json; charset=UTF-8")
    public R<Object> getDefaultAddress(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName
    ) {
        DeliveryAddress address = deliveryAddressService.getDefault(storeUid, staffUid);
        DeliveryAddressDto addressDto = null;
        if (address != null) {
            addressDto = OrderFormV3Util.getAddressDto(address);
        }
        return R.ok(addressDto);
    }

    @ApiOperation(value = "删除地址")
    @PostMapping(value = "/drop", produces = "application/json; charset=UTF-8")
    public R<Object> drop(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        DeliveryAddress address = deliveryAddressService.getById(id);
        if (address == null) return R.fail("获取失败");

        deliveryAddressService.deleteByStaffIdAndId(staffUid, id);

        return R.ok();
    }
}
